package com.brokendust.client.api.module.image;

import com.brokendust.client.PixelEngine;
import com.brokendust.client.api.module.GUIComponent;
import com.brokendust.client.api.module.PullPanelComponent;
import com.brokendust.client.api.util.BasePosition;
import com.brokendust.client.api.util.image.ImageControl;
import com.brokendust.client.config.module.ComponentJson;
import com.brokendust.client.config.module.image.ImageShowJson;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class ImageShow extends AbstractGui implements GUIComponent, PullPanelComponent {
    public int guiWidth;
    public int guiHeight;
    public int guiDWidth;
    public int guiDHeight;
    public ResourceLocation image;
    public int objectWidth;
    public int objectHeight;
    public int x;
    public int y;
    public int position;
    public String object_id;

    public ImageShow(ImageShowJson imageShowJson, int guiWidth, int guiHeight, int guiDWidth, int guiDHeight) {
        this.guiWidth = guiWidth;
        this.guiHeight = guiHeight;
        this.guiDWidth = guiDWidth;
        this.guiDHeight = guiDHeight;
        this.object_id = imageShowJson.getObject_name();
        this.image = ImageControl.getResourceLocation(imageShowJson.getImage());
        this.objectWidth = imageShowJson.getWidth();
        this.objectHeight = imageShowJson.getHeight();
        this.x = imageShowJson.getX();
        this.y = imageShowJson.getY();
        this.position = imageShowJson.getPosition();
    }

    public ImageShow(ImageShowJson imageShowJson) {
        this.image = ImageControl.getResourceLocation(imageShowJson.getImage());
        this.objectWidth = imageShowJson.getWidth();
        this.objectHeight = imageShowJson.getHeight();
        this.x = imageShowJson.getX();
        this.y = imageShowJson.getY();
        this.position = imageShowJson.getPosition();
    }

    @Override
    public void upData(ComponentJson componentJson) {
        if (!(componentJson instanceof ImageShowJson)) {
            return;
        }
        ImageShowJson imageShowJson = (ImageShowJson) componentJson;
        this.image = ImageControl.getResourceLocation(imageShowJson.getImage());
        this.objectWidth = imageShowJson.getWidth();
        this.objectHeight = imageShowJson.getHeight();
        this.object_id = imageShowJson.getObject_name();
        this.x = imageShowJson.getX();
        this.y = imageShowJson.getY();
        this.position = imageShowJson.getPosition();
    }

    @Override
    public void renderGUI(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick, int guiX, int guiY) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        PixelEngine.minecraft.getTextureManager().bind(this.image);
        Integer[] integers = BasePosition.getObjectPoint(guiX, guiY, this.guiWidth, this.guiHeight, this.objectWidth, this.objectHeight, this.x, this.y, this.position);
        int mX = integers[0];
        int mY = integers[1];
        AbstractGui.blit(matrixStack, mX, mY, 0.0f, 0.0f, this.objectWidth, this.objectHeight, this.objectWidth, this.objectHeight);
    }

    @Override
    public void renderPull(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick, int guiX, int guiY, int displayX, int displayY) {
        Integer[] integers = BasePosition.getObjectPoint(guiX, guiY, this.guiWidth, this.guiHeight, this.objectWidth, this.objectHeight, this.x, this.y, this.position);
        int mX = integers[0];
        int mY = integers[1];
        int mmX = mX + this.objectWidth;
        int mmY = mY + this.objectHeight;
        int ddX = displayX + this.guiDWidth;
        int ddY = displayY + this.guiDHeight;
        boolean withinRange = mX >= displayX - this.objectWidth && mX <= ddX && mmX >= displayX && mmX <= ddX + this.objectWidth;
        boolean withinRange2 = mY >= displayY - this.objectHeight && mY <= ddY && mmY >= displayY && mmY <= ddY + this.objectHeight;
        if (withinRange && withinRange2) {
            int cutX = 0;
            int cutMX = 0;
            if (mX < displayX) {
                cutX = displayX - mX;
                cutMX = displayX - mX;
                if (cutX > this.objectWidth) {
                    cutX = this.objectWidth;
                    cutMX = this.objectWidth;
                }
            }
            if (mmX > ddX) {
                cutX = mmX - ddX;
                if (cutX > this.objectWidth) {
                    cutX = this.objectWidth;
                }
            }
            int cutY = 0;
            int cutMY = 0;
            if (mY < displayY) {
                cutY = displayY - mY;
                cutMY = displayY - mY;
                if (cutY > this.objectHeight) {
                    cutY = this.objectHeight;
                    cutMY = this.objectHeight;
                }
            }
            if (mmY > ddY) {
                cutY = mmY - ddY;
                if (cutY > this.objectHeight) {
                    cutY = this.objectHeight;
                }
            }
            RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
            PixelEngine.minecraft.getTextureManager().bind(this.image);
            AbstractGui.blit(matrixStack, mX + cutMX, mY + cutMY, (float) cutMX, (float) cutMY, this.objectWidth - cutX, this.objectHeight - cutY, this.objectWidth, this.objectHeight);
        }
    }

    public void renderHUD(MatrixStack matrixStack, int guiX, int guiY) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        PixelEngine.minecraft.getTextureManager().bind(this.image);
        Integer[] integers = BasePosition.getObjectHubPoint(guiX, guiY, this.objectWidth, this.objectHeight, this.x, this.y, this.position);
        int mX = integers[0];
        int mY = integers[1];
        AbstractGui.blit(matrixStack, mX, mY, 0.0f, 0.0f, this.objectWidth, this.objectHeight, this.objectWidth, this.objectHeight);
    }
}
